from time import sleep
from selenium.webdriver.common.by import By


class PageEcshopBackRedEnvelopes():
    ''' 后台红包发送 '''

    def __init__(self, driver):
        self.driver = driver
        # 元素定位器
        self.loc_red_envelopes_add = (By.LINK_TEXT, "添加红包类型")  # 添加红包类型按钮
        self.loc_red_envelopes_send = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[2]/td[7]/a[1]')  # 发红包按钮
        self.loc_red_envelopes_remove = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[2]/td[7]/a[4]')  # 移除红包按钮

        self.loc_red_envelopes_name = (By.XPATH, '//input[@name="type_name"]')  # 红包类型
        self.loc_red_envelopes_money = (By.XPATH, '//input[@name="type_money"]')  # 发红包按
        self.loc_red_envelopes_min_amount = (By.XPATH, '//input[@name="min_goods_amount"]')  # 发红包按钮
        self.loc_red_envelopes_submit = (By.XPATH, '/html/body/div[1]/form/table/tbody/tr[10]/td[2]/input[1]')  # 发红包按钮

        self.loc_return_envelopes = (By.LINK_TEXT, '返回红包类型列表')

        self.loc_red_send_keyword = (By.NAME, "keyword")  # 搜索用户框
        self.loc_red_send_search = (By.NAME, 'search')  # 搜索按钮
        self.loc_red_send_res = (By.XPATH, '//*[@id="user_search"]/option[1]')  # 选择搜索结果第一条
        self.loc_red_send_import = (
            By.XPATH, "/html/body/div[2]/form/div[2]/table/tbody/tr[2]/td[2]/p[1]/input")  # 导入结果框
        self.loc_red_send_submit = (By.NAME, "send_user")  # 确认按钮

        self.loc_red_lopes_remove_assert = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[2]/td[1]/span')  # 移除后断言内容

    def red_envelopes_add(self):
        ''' 进入红包类型添加 '''
        self.driver.find_element(*self.loc_red_envelopes_add).click()

    def red_envelopes_send(self):
        ''' 进入发送红包的页面 '''
        self.driver.find_element(*self.loc_red_envelopes_send).click()

    def red_envelopes_remove(self):
        ''' 删除红包 '''
        self.driver.find_element(*self.loc_red_envelopes_remove).click()
        self.driver.switch_to.alert.accept()

    def red_envelopes_remove_assert(self):
        ''' 删除的断言 '''
        act = self.driver.find_element(*self.loc_red_lopes_remove_assert).text
        return act

    def return_envelopes(self):
        ''' 添加红包后返回红包列表 '''
        self.driver.find_element(*self.loc_return_envelopes).click()

    def red_envelopes_name(self, type_name):
        ''' 红包类型输入 '''
        self.driver.find_element(*self.loc_red_envelopes_name).send_keys(type_name)

    def red_envelopes_money(self, type_money):
        ''' 红包金额输入 '''
        self.driver.find_element(*self.loc_red_envelopes_money).send_keys(type_money)

    def red_envelopes_min_amount(self, min_goods_amount):
        ''' 红包最小金额输入 '''
        self.driver.find_element(*self.loc_red_envelopes_min_amount).send_keys(min_goods_amount)

    def red_envelopes_submit(self):
        ''' 红包最小金额输入 '''
        self.driver.find_element(*self.loc_red_envelopes_submit).click()

    def red_send_keyword(self, keyword):
        ''' 发送红包 搜索用户框'''
        self.driver.find_element(*self.loc_red_send_keyword).send_keys(keyword)

    def red_send_search(self):
        ''' 发送红包 搜索用户按钮'''
        self.driver.find_element(*self.loc_red_send_search).click()

    def red_send_res(self):
        ''' 发送红包 搜索用户结果框 '''
        self.driver.find_element(*self.loc_red_send_res).click()

    def red_send_import(self):
        ''' 发送红包 结果导出按钮 '''
        self.driver.find_element(*self.loc_red_send_import).click()

    def red_send_submit(self):
        ''' 发送红包 最终确定按钮'''
        self.driver.find_element(*self.loc_red_send_submit).click()

    def red_add(self, type_name, type_money, min_goods_amount):
        self.red_envelopes_name(type_name)
        self.red_envelopes_money(type_money)
        self.red_envelopes_min_amount(min_goods_amount)
        self.red_envelopes_submit()
        self.return_envelopes()

    def red_send(self, username):
        self.red_send_keyword(username)
        self.red_send_search()
        sleep(1)
        self.red_send_res()
        self.red_send_import()
        self.red_send_submit()
